package com.ncloud.crm.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ncloud.crm.entity.BsProject;
@Mapper
public interface BsProjectMapper {
	
	@Insert("insert into bs_project(project_id, cust_id, project_name, contact_name, contact_phone, test_flag, state, "
			+ "manager, create_time, create_user, destory_time, update_time, update_user, remark)"
            + " values(#{projectId},#{custId},#{projectName},#{contactName},#{contactPhone},#{testFlag},#{state},"
            + "#{manager},#{createTime},#{createUser},#{destoryTime},#{updateTime},#{updateUser},#{remark})")
	int insertBsProject(BsProject project);
	
	@Update("update bs_project set project_name=#{projectName},contact_name=#{contactName},contact_phone=#{contactPhone},"
			+ "test_flag=#{testFlag},state=#{state},manager=#{manager},update_time=#{updateTime},update_user=#{updateUser},"
			+ "destory_time=#{destoryTime},remark=#{remark} where project_id=#{projectId}")
	int updateBsProject(BsProject project);

	@Select("select * from bs_project where project_id = #{projectId} and state = '1'")
	@Results({ @Result(column = "project_id", property = "projectId"), 
			@Result(column = "cust_id", property = "custId"),
			@Result(column = "project_name", property = "projectName"),
			@Result(column = "contact_name", property = "contactName"),
			@Result(column = "contact_phone", property = "contactPhone"),
			@Result(column = "test_flag", property = "testFlag"), @Result(column = "state", property = "state"),
			@Result(column = "manager", property = "manager"), @Result(column = "create_time", property = "createTime"),
			@Result(column = "create_user", property = "createUser"),
			@Result(column = "destory_time", property = "destoryTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark") })
	BsProject getProjectByPk(Long projectId);
	
	@Select("select * from bs_project where cust_id = #{0} and manager = #{1} and state = '1'")
	@Results({ @Result(column = "project_id", property = "projectId"), 
			@Result(column = "cust_id", property = "custId"),
			@Result(column = "project_name", property = "projectName"),
			@Result(column = "contact_name", property = "contactName"),
			@Result(column = "contact_phone", property = "contactPhone"),
			@Result(column = "test_flag", property = "testFlag"), @Result(column = "state", property = "state"),
			@Result(column = "manager", property = "manager"), @Result(column = "create_time", property = "createTime"),
			@Result(column = "create_user", property = "createUser"),
			@Result(column = "destory_time", property = "destoryTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark") })
	 List<BsProject> getProjectByCustIdAndManager(String custId,String manager);
	/*
	* 查询我的项目信息：根据销售id，现在客户和用户还没有做关联
	* */
	@Select(value = {"select  a.*,b.cust_name from bs_project a , bs_cust b where b.cust_id=a.cust_id and a.state='1' and b.cust_state='1' and a.manager=#{0}"})
	@Results({ @Result(javaType = String.class, column = "project_id", property = "projectId"),
			@Result(javaType = String.class, column = "cust_id", property = "custId"),
			@Result(column = "project_name", property = "projectName"),
			@Result(column = "contact_name", property = "contactName"),
			@Result(column = "contact_phone", property = "contactPhone"),
			@Result(column = "test_flag", property = "testFlag"), @Result(column = "state", property = "state"),
			@Result(javaType = String.class, column = "manager", property = "manager"), @Result(column = "create_time", property = "createTime"),
			@Result(javaType = String.class, column = "create_user", property = "createUser"),
			@Result(column = "destory_time", property = "destoryTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(javaType = String.class, column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark"),
			@Result(column = "cust_name", property = "custName")
	})
	public List<Map<String, Object>> getBsProjectBySale(Long saleID);

}